package com.bee.caf_ss_android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.bee.baselibrary.base.BaseActivity;
import com.bee.baselibrary.utils.T;
import com.bee.baselibrary.weight.TitleBar;
import com.bee.caf_ss_android.common.Config;
import com.bee.caf_ss_android.common.GeneralReturnEntity;
import com.bee.caf_ss_android.common.LoadingDialog;
import com.bee.caf_ss_android.common.LocalStorage;
import com.bee.caf_ss_android.common.Tools;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author LiHao 南柯一梦宁沉沦 不梦前程不梦卿
 * @org www.fengjishi.com (重庆蜂效电子商务有限公司)
 * @data Created time 2019/4/3.
 * @email 930176087@qq.com
 * @pakeage com.bee.caf_ss_android
 * Personal signature 余生还长 何必慌张
 * Page description : 个人信息
 */
public class PersonalDataActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;

    private String token , counselorId ;

    private LocalStorage localStorage ;
    private LoadingDialog loadingDialog ;

    private Activity activity ;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_personal_data;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        loadingDialog = new LoadingDialog(this,R.style.DialogStyle) ;
        localStorage = new LocalStorage(this) ;
        activity = this ;

        init();
    }

    private void init(){

        token = localStorage.getToken() ;
        counselorId = localStorage.getCounselorId() ;

    }
    /**
     * 点击退出登录
     * @param view
     */
    public void onClickLogout(View view){

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("提示") ;
        normalDialog.setMessage("是否确认退出当前用户");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        logout() ;
                    }
                });
        normalDialog.setNegativeButton("取消",null);
        // 显示
        normalDialog.show();


    }
    private void logout(){
        RequestParams requestParams = new RequestParams(Config.InterfaceHost+"app/logout") ;
        requestParams.addBodyParameter("token",token) ;
        requestParams.addBodyParameter("counselorId",counselorId) ;

        loadingDialog.show();
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                GeneralReturnEntity entity = JSONObject.parseObject(result,GeneralReturnEntity.class) ;

                //无论结果如何，都跳转登录页面
                localStorage.cleanAll();
                //关闭上一个页面，否则
                PersonalActivity.instance.finish();
                Tools.refPage(activity,LoginActivity.class,true);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                loadingDialog.dismiss();
            }
        }) ;
    }
}
